package com.sensePlusVR.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

import com.sensePlusVR.utils.ContextUtils;

import java.util.Timer;
import java.util.TimerTask;

public class MarqueeHorTextView extends TextView {
	private Context context;

	public MarqueeHorTextView(Context context) {
		super(context);
		this.context = context;
	}

	public MarqueeHorTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;

	}

	public MarqueeHorTextView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
	}

	@Override
	@ExportedProperty(category = "focus")
	public boolean isFocused() {

		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		timeTask();

	}

	private TimerTask task;

	private Timer timer = new Timer();

	/**
	 * 当不执行onDraw方法的时候则判定Text已经走到头部
	 */
	private void timeTask() {

		if (task != null) {

			task.cancel();

			task = null;

		}

		task = new TimerTask() {
			public void run() {

				Activity activity = ContextUtils.scanForActivity(context);

				if (activity == null)
					return;

				activity.runOnUiThread(new Runnable() {

					@Override
					public void run() {

						setEllipsize(TruncateAt.END);

					}
				});

			}
		};

		timer.schedule(task, 300);
	}
}
